import re
import requests
import pymysql

"""
<div class="productImg-wrap">
<img src="([\S\s]*?)" data-spm-anchor-id="[\S\s]*?">[\s\S]*?
<em title="(.*?)"><b>?</b>.*?</em>[\S\s]*?
<a href="[\S\s]*?" target="_blank" title="([\S\s]*?)" data-p="[\s\S]*?" data-spm-anchor-id="[\S\s]*?" atpanel="[\s\S]*?">[\s\S]*?
<span>月成交 <em>(.*?)</em></span>
"""

def take_info(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
    r = requests.get(url, headers=headers)
    r.encoding = r.apparent_encoding
    html = r.text
    regex = '<div class="product-iWrap">[\S\s]*?' \
            '<img  data-ks-lazyload=  "([\S\s]*?)" />[\s\S]*?' \
            '<em title="(.*?)"><b>.*?</b>.*?</em>[\S\s]*?' \
            '<a href="[\S\s]*?" target="_blank" title="(.*?)" data-p=".*?" >[\S\s]*?' \
            '<span>月成交 <em>(.*?)</em></span>[\S\s]*?' \
            '<span>评价 <a href="[\s\S]*?">(.*?)</a></span>'
    # regex1 = '<img  data-ks-lazyload=  "([\S\s]*?)" />'
    # regex2 = '<em title="(.*?)"><b>.*?</b>.*?</em>'
    # regex3 = '<a href="[\S\s]*?" target="_blank" title="(.*?)" data-p=".*?" >'
    # regex4 = '<span>月成交 <em>(.*?)</em></span>'
    info = re.findall(regex, html)
    # print(info)
    # image_url = ['http:' + i[0] for i in info]
    # price = [i[1] for i in info]
    # name = [i[2] for i in info]
    # dindan = [i[3] for i in info]
    # for i in info:
    #     try:
    #         conn = pymysql.connect('www.ccpython.cn', 'root', 'root', 'cloth', 3306)
    #         cursor = conn.cursor()
    #         cursor.execute("""insert into goods values(null, now(), now(), 0, '%s', '%s', 1, '%s', '%s', '%s', '%s', 4, 5, 4)""" % (i[2], 'http:' + i[0], 1650, i[3], i[1], i[4]))
    #         conn.commit()
    #         print('保存成功!!!')
    #     except Exception as e:
    #         print(e)
if __name__ == '__main__':
    # 男装
    # url = 'https://list.tmall.com/search_product.htm?q=%C4%D0%D7%B0&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton'
    # url = 'https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.9.45931602ej1Vzo&s=60&q=%C4%D0%D7%B0&sort=s&style=g&from=.list.pc_1_searchbutton&type=pc#J_Filter'

    # 女装
    # url = 'https://list.tmall.com/search_product.htm?q=%C5%AE%D7%B0&type=p&spm=a220m.1000858.a2227oh.d100&from=.list.pc_1_searchbutton'
    # url = 'https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.9.79165c21VbsHPV&s=60&q=%C5%AE%D7%B0&sort=s&style=g&from=.list.pc_1_searchbutton&type=pc#J_Filter'

    # 童装
    # url = 'https://list.tmall.com/search_product.htm?q=%CD%AF%D7%B0&type=p&spm=a220m.1000858.a2227oh.d100&from=.list.pc_1_searchbutton'
    # url = 'https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.9.599c65a9k9sTC3&s=60&q=%CD%AF%D7%B0&sort=s&style=g&from=.list.pc_1_searchbutton&type=pc#J_Filter'

    # 包包
    # url = 'https://list.tmall.com/search_product.htm?q=%CA%B1%C9%D0%B0%FC%B0%FC&type=p&spm=a220m.1000858.a2227oh.d100&from=.list.pc_1_searchbutton'
    # url = 'https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.9.bc972b9fDdYQzX&s=60&q=%B0%FC%B0%FC&sort=s&style=g&from=.list.pc_1_searchbutton&type=pc#J_Filter'

    # 鞋靴
    # url = 'https://list.tmall.com/search_product.htm?q=%D0%AC%D1%A5&type=p&spm=a220m.1000858.a2227oh.d100&from=.list.pc_1_searchbutton'
    url = 'https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.9.39381be2994gug&s=60&q=%D0%AC%D1%A5&sort=s&style=g&from=.list.pc_1_searchbutton&type=pc#J_Filter'

    take_info(url)

